package figure8.GUI;

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

import figure8.Controller;
import figure8.Seq;

public class NotSavedWindow extends JDialog {
	public NotSavedWindow(final Controller controller, final int option)
	{
		this.setLocation(controller.getWindow().getFrame().getX() + 80, controller.getWindow().getFrame()
				.getY() + 120);
		
		final JDialog warningWindow = this;	
		JPanel panel = new JPanel(new GridLayout(3,1));
		panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		this.getContentPane().add(panel);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage("src/figure8/GUI/Fig8-small.png"));
		this.setAlwaysOnTop(true);
		panel.add(new JLabel("The current changes have not been saved."));
		panel.add(new JLabel("Changes will be lost, do you wish to save?"));

		JPanel buttons = new JPanel(new GridLayout(1,2));
		JButton yes = new JButton("Yes");
		yes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controller.save();
				warningWindow.dispose();
				switch (option)
				{
					case 0: controller.newWin(); break;
					case 1: controller.openWin(); break;
					case 2: System.exit(0); break;
					case 3: controller.closeProject(); break;
					case 4: controller.newOpenController(); break;
					case 5: controller.newController(); break;
					case 6: controller.openSequencer2(); break;
				}
			}
		});
		buttons.add(yes);
		JButton no = new JButton("No");
		no.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				warningWindow.dispose();
				switch (option)
				{
					case 0: controller.newWin();
					case 1: controller.openWin();
					case 2: System.exit(0);
					case 3: controller.closeProject();
					case 4: controller.newOpenController(); break;
					case 5: controller.newController(); break;
					case 6: controller.openSequencer2(); break;
				}
			}
		});
		buttons.add(no);
		panel.add(buttons);

		this.pack();
		this.setVisible(true);
	}
	
	public NotSavedWindow(final Seq seq)
	{
		this.setLocation(seq.getWindow().getPanel().getX() + 80, seq.getWindow().getPanel()
				.getY() + 120);
		
		final JDialog warningWindow = this;	
		JPanel panel = new JPanel(new GridLayout(3,1));
		panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		this.getContentPane().add(panel);
		
		panel.add(new JLabel("The current changes have not been saved."));
		panel.add(new JLabel("Changes will be lost, do you wish to save?"));

		JPanel buttons = new JPanel(new GridLayout(1,2));
		JButton yes = new JButton("Yes");
		yes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				seq.save();
				warningWindow.dispose();
				System.exit(0);
			}
		});
		buttons.add(yes);
		JButton no = new JButton("No");
		no.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				warningWindow.dispose();
				System.exit(0);
			}
		});
		buttons.add(no);
		panel.add(buttons);

		this.pack();
		this.setVisible(true);
	}
}